import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {timer} from 'rxjs';

@Component({
  selector: 'app-proxy',
  templateUrl: './app-proxy.component.html',
  styleUrls: ['./app-proxy.component.css']
})
export class AppProxyComponent implements OnInit {
  apps: any[];
  status: boolean;
  @Output() success = new EventEmitter();
  @Output() failed = new EventEmitter();
  constructor() { }

  ngOnInit() {
    // this.apps = [
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 0},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 1},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 1},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 0},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 0},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 1},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 1},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 0},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 0},
    //   {appName: '360手机助手', packageName: 'com.360.www', isSelected: 1}
    // ];
  }

  changeProxyStatus(event) {
    this.status = event;
  }

  changeStatus(app: any) {
    if (app.isSelected === 0) {
      app.isSelected = 1;
    } else {
      app.isSelected = 0;
    }
  }

  changeAppProxy() {
    let packages = '';
    for (const app of this.apps) {
      if (app.isSelected === 1) {
        packages = packages + app.packageName + '==';
      }
    }
    timer(100).subscribe(() => {
      this.success.emit({proxyStatus: this.status + '', proxyPackages: packages});
    });
  }

}
